<?php 
class incoming_model extends CI_Model
{
	public function getLastInserted($table,$id)
		{
			$this->db->select_max($id);
			$Q = $this->db->get($table);
			$row = $Q->row_array();
			return $row[$id];
		}
	public function isEqual($val1,$val2){
		if($val1 == $val2){
			return true;
		}else{
			return false;
		}
	}
	public function getWorklist(){
		
		$query = $this->db->get('masterTemplate');
		 
		return json_encode($query->result());
	}
	public function keyInInComing($inputData,$inputAllData){
		 // check data in masterTemplate
		 $findMasterRealTable = array(
		 			"form_id"=>"6",
					"type_of"=>$inputData["type_of"],
					"brand"=>$inputData["brand"],
					"lot_no"=>$inputData["lot_no"],
					"grade"=>$inputData["grade"],
		 						);
		 $this->db->where($findMasterRealTable);
		 $query = $this->db->get('masterTemplate');
		if($query->num_rows() > 0)
		{
			$rs = $query->result();
			$get_mt_id = $rs[0]->mt_id;   // หาค่า id เพื่อจะนำไปหา data ในตาราง dataTemplate

			$trasactionData = array(
					"form_id"=>"6",
					"mt_id"=>$get_mt_id,
					"type_of"=>$inputData["type_of"],
					"brand"=>$inputData["brand"],
					"lot_no"=>$inputData["lot_no"],
					"grade"=>$inputData["grade"],
					"modify_date"=>date("Y-m-d H:i:s"),
					//"status"=>1,
					"modify_user"=>$this->session->userdata('user_id')
			);
			$this->db->insert("transactionData",$trasactionData);
			$myID = $this->getLastInserted('transactionData','t_id');
			$transactionStatus=1;
			foreach ($inputAllData as $key){
				$findData = array(
								//"form_id"=>"1",
								"mt_id"=>$get_mt_id,
								"data_name"=>$key["data_name"]
						);
				$this->db->where($findData );
				$query = $this->db->get('dataTemplate');
				$dataTemplate = $query->result();
				$checkData = $dataTemplate[0];
				
				if(!(($checkData->value_start == 0) && ($checkData->value_end ==0))){
					
				
					if($this->isEqual($checkData->value_start,$checkData->value_end)){
						if($key["data_value"] == $checkData->value_start){
							$status= "1";
						}else{
							$status=  "0";
							
							$transactionStatus=0;  // ตรวจสอบว่าผิดเงื้อนไขหรือไม่ ถ้าผิดให้ไม่ผ่าน คือ status เป็น 0
						}
					}else{
						if(($key["data_value"] >= $checkData->value_start  ) && ($key["data_value"] <= $checkData->value_end ) ){
							$status=  "1";
						}else{
							$status=  "0";       // ตรวจสอบว่าผิดเงื้อนไขหรือไม่ ถ้าผิดให้ไม่ผ่าน คือ status เป็น 0
							$transactionStatus=0;
						}					
					}
				}else{
					$status=  "1";
				}
				$saveToDataValue = array(
									    "t_id"=>$myID,
										"data_name"=>$key["data_name"],
										"data_value"=>$key["data_value"],
										"status" => $status,
										"modify_time"=>date("Y-m-d H:i:s")
									);
				$this->db->insert("dataValue",$saveToDataValue);
				
				///
				if($transactionStatus == 1  ){
					//$transactionStatus=1;
					$outPut = "บันทึกข้อมูลเรียบร้อยแล้ว ผ่านการทดสอบ";
				}else{
					$outPut = "บันทึกข้อมูลเรียบร้อยแล้ว ไม่ผ่านการทดสอบ";
				}
				$updateStatus = array( "status"=>$transactionStatus);
				$this->db->where("t_id",$myID);
				$this->db->update("transactionData",$updateStatus);
			}
			return $outPut;

		}else
		{
			return "ไม่พบค่าใน Database";
		}
	//	echo $this->db->last_query();
		/////
	
	}
	public function checkInComing($inputData,$inputAllData)
	{
		 $this->db->where($inputData);//ตรวจสอบค่าซ้ำ
		 $query = $this->db->get('masterTemplate');
		 $myID = $this->getLastInserted('masterTemplate','mt_id');
		   
		 if($query->num_rows() == 0 )
		 {
		 	$inputData["modify_date"]= date("Y-m-d H:i:s");//เพิ่มตัวแปรเวลา
		 	$this->db->insert("masterTemplate",$inputData);
		 	foreach($inputAllData as $key)
			{
		 		$key["mt_id"]=  $myID+1;
		 		$key["status"]=  "1";
		 		$key["modify_time"]=date("Y-m-d H:i:s");//ค่าเวลาในฐานข้อมูล
		 		$this->db->insert("dataTemplate",$key);
		 		}
		 	 return "เพิ่มข้อมูล การตั้งค่าเรียบร้อยแล้ว!";
		 		}
		 		else
					 {
		  				return "ไม่สามารถเพิ่มข้อมูได้ มีข้อมูลอยู่แล้ว!!";
					 }
	}
		public function detail_incoming1($t_id)
		{
			$this->db->where('t_id',$t_id);
			$rs=$this->db->get("transactionData");
			return $data['rs1'] = $rs->result_array();
		}
		public function detail_incoming2($t_id)
		{
			$this->db->where('t_id',$t_id);
			$rs=$this->db->get("dataValue");
			return $data['rs2'] = $rs->result_array();
		}
		public function information_incoming1($mt_id)
		{
			$this->db->where('mt_id',$mt_id);
			$rs=$this->db->get("masterTemplate");
			return $data['rs1'] = $rs->result_array();
		}
		public function information_incoming2($mt_id)
		{
			$this->db->where('mt_id',$mt_id);
			$rs=$this->db->get("dataTemplate");
			return $data['rs2'] = $rs->result_array();
		}
		//--//
		public function list_incoming($t_id)
		{   
			if($this->session->userdata('admin')==0 ){ // ตรวจสอบว่า ถ้าไม่ใช่ admin ให้แสดงตาม id user 
				$this->db->where('modify_user',$this->session->userdata('user_id'));
			}
			$this->db->order_by('t_id','DESC');
			$query = $this->db->get($t_id);
			//return $data['rs'] = $rs->result_array();

			$i=1;
			$out_data = array(); // กำหนดค่าใหม่ให้กับ array
			foreach($query->result_array() as $key ){
				// echo "<pre>";
				$key["form_name"] = $this->application_model->checkFormName($key["form_id"]);
				$key["order_id"] = $i;
				$key["user_keyin"] = $this->user_model->getFullName($key["modify_user"]);
				 if($key["status"] == 0 ){
					$key["status_text"] = "Not Completed";
				}else{
					$key["status_text"] = "Completed";
				}
				$out_data[] = $key;
				//  print_r($key);
				$i++;
			}
			return $out_data;

		}
}
?>